/*
 * Copyright 2012 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.mobile.view.velocity.tools;

import com.fujitsu.nanjing.mobile.common.PropertyUtils;

import org.apache.velocity.runtime.log.Log;
import org.apache.velocity.tools.config.DefaultKey;
import org.apache.velocity.tools.generic.SafeConfig;
import org.apache.velocity.tools.generic.ValueParser;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Scope;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

/**
 * DomainTool.java
 * 
 * @author Qixiaobo-win8
 */
@DefaultKey("domain")
@Scope
@DependsOn
public class DomainTool extends SafeConfig {
    private static Properties context;
    protected Log LOG;

    static {
        Resource r = new ClassPathResource(
                "com/fujitsu/nanjing/mobile/props/context.properties");
        try {
            context = PropertyUtils.readFromResource(r);
        } catch (IOException e) {
            context = new Properties();
        }
    }

    public String info(String key) {
        return context.getProperty(key);
    }

    public String getCopyrightYear() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        return String.valueOf(c.get(Calendar.YEAR));
    }

    public String getDomain() {
        return info("domain");
    }

    public String get() {
        return getDomain();
    }

    @Override
    protected void configure(ValueParser props) {
        LOG = (Log) props.getValue("log");
    }

    public String fileSize(String size) {
        double sizeNum = Integer.parseInt(size);
        String[] list = new String[] { "B", "KB", "MB", "GB", "TB" };
        int position = 0;
        while (sizeNum >= 1024) {
            sizeNum /= 1024.0;
            position++;
        }
        return Math.round(sizeNum) + " " + list[position];
    }

    public String companySize(int size) {
        String str = null;
        switch (size) {
        case 1:
            str = "<span title='less than 10 employees' data-rel='tooltip'>small</span>";
            break;
        case 2:
            str = "<span title='10-100 employees' data-rel='tooltip'>medium</span>";
            break;
        case 3:
            str = "<span title='more than 100 employees' data-rel='tooltip'>large</span>";
            break;
        default:
            str = "<span title='sorry,we cannot get your company size' data-rel='tooltip'>unkonwn size</span>";
            break;
        }
        return str;
    }
    public String companyState(int state) {
        String str = null;
        switch (state) {
        case 1:
            str = "normal";
            break;
        case 2:
            str = "abnormal";
            break;
        default:
            str = "unkonwn";
            break;
        }
        return str;
    }
    public String gender(int gender) {
        String str = null;
        switch (gender) {
        case 1:
            str = "Man";
            break;
        case 2:
            str = "Lady";
            break;
        default:
            str = "unkonwn";
            break;
        }
        return str;
    }
}
